/*
*
==============================================================================
*  Name        : 
*  Part of     : Omen Game Engine - DirectX 10 Plugin
*  Description : 
*  Version     : 1.0
*
*  Copyright (c) 2013 Lauri Kortevaara.
* 
==============================================================================
*/

#ifndef _DirectX_10_INPUT_DRIVER_H_
#define _DirectX_10_INPUT_DRIVER_H_

#define DIRECTINPUT_VERSION 0x0800

#include <OmenEngine>
#include "Config.h"

/////////////
// LINKING //
/////////////
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")

//////////////
// INCLUDES //
//////////////
#include <dinput.h>

namespace Omen {

    class MouseInputDevice;
    class KeyboardInputDevice;

    /// DirectX_10_ Input drver
    class DirectX_10_InputDriver : public Omen::IInputDriver
    {
    /// Constructors and destructor
    public: 
        DIRECTX_10_PLUGIN_API DirectX_10_InputDriver(); /// <summary>Creates an DirectX_10_ Input driver</summary>
        DIRECTX_10_PLUGIN_API DirectX_10_InputDriver(const DirectX_10_InputDriver&); /// <summary>Creates an DirectX_10_ Input driver</summary>
        DIRECTX_10_PLUGIN_API virtual ~DirectX_10_InputDriver(); /// <summary>Destroys an DirectX_10_ Input driver</summary>

    /// Omen::IInputDriver interface
    public:
        virtual const std::wstring &getName() const;       /// <summary>Gets the name of the Input driver</summary>
        virtual unsigned int deviceCount() const;
        
        virtual std::vector< IInputDevice* > createInputDevices(unsigned long windowHandle); /// <summary>Creates the input device using the driver's API</summary>

        virtual void handleInput() const;

    /// DirectX_10_InputDriver implementation
    public:
        void createMouseInputDevice( const GUID& guid );
        void createKeyboardInputDevice( const GUID& guid );

        /// IDirectInput8 interface
        static BOOL FAR PASCAL DIEnumDevicesCallback( LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef ); /// <summary>Callback function for input device enumeration</summary>


    private:
        MouseInputDevice*           m_pMouseInputDevice;
        KeyboardInputDevice*        m_pKeyboardInputDevice;

        std::vector< IInputDevice* > m_devices;

        IDirectInput8* m_directInput;

        HWND    m_hwnd;
        std::wstring m_name;
    };


    class MouseInputDevice : public Omen::IInputDevice
    {
        public: 
            MouseInputDevice(HWND hwnd, const GUID& guid, IDirectInput8* pDirectInput);
            virtual ~MouseInputDevice();
        public: 
            virtual bool handleInput();

        private:
            IDirectInputDevice8* m_mouse;
            DIMOUSESTATE         m_mouseState;

            int                  m_mouseX, m_mouseY, m_mouseZ;
            int                  m_screenWidth, m_screenHeight;
            HWND                 m_hwnd;
    };

    class KeyboardInputDevice : public Omen::IInputDevice
    {
        #define KEYBOARD_BUFFER_SIZE 256
        public: 
            KeyboardInputDevice(HWND hwnd, const GUID& guid, IDirectInput8* pDirectInput);
            virtual ~KeyboardInputDevice();
        public: 
            virtual bool handleInput();

        private:
            IDirectInputDevice8* m_keyboard;
            BYTE                 m_keyboardState[KEYBOARD_BUFFER_SIZE];
            HWND                 m_hwnd;
    };

} // namespace Omen

#endif // _DirectX_10_INPUT_DRIVER_H_